/**
 * 
 */
Ext.define('Case.Sub', {
	extend: 'Ext.grid.Panel',
	alias: 'case.sub',
	closable: true,
    
    initComponent: function() {
    	var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
            clicksToEdit: 1
        });
    	Ext.apply(this, {
    		store: this.store(),
    		columns:[{
    			text:'Name',
    			dataIndex:'name'
    		},{
    			text:'Value',
    			dataIndex:'value',
    			field: {
                    allowBlank: false
                }
    		}],
    		plugins: [cellEditing]
    	});
    	this.callParent(arguments);
	},

	store: function(){
		this.store = Ext.create('Ext.data.Store', {
			model: 'Param',
			data: [] /*
			       {name:'n1', value:'v1'},
			       {name:'n2', value:'v2'}
			]*/
		});
		return this.store;
	},
	
	load: function(subcase){
		var me_subcase = this;
		me_subcase.model = subcase;
		var data = new Array();
		subcase.params().each(function(param){
			data[data.length] = param;
		});
		me_subcase.store.loadData(data, false);
	},
	
	getSubCaseModel: function() {
		var me_subcase = this;
		var subcase = me_subcase.model;
		if (!subcase) {
			subcase = Ext.ModelManager.create({params:[]}, 'SubCase');
		}
		else{
			subcase.params().removeAll();
		}
		subcase.data.params = [];
		subcase.set('name', me_subcase.title);
		me_subcase.store.each(function(param) {
			//subcase.params().add(param);
			subcase.data.params.push(param.data);
		});
		if (subcase.data.params.length==0) {
			subcase.data.params = {};
		}
		return subcase;
	},
	
	setParamNames: function(paramNames) {
		var me_subcase = this;
		var data = [];
		for (var idx = 0; idx < paramNames.length; ++idx) {
			var name = paramNames[idx];
			if (name.charAt(0) == '_') {
				/*to do something here*/
				/*_RECENTLY_;_ANY_*/
				continue;
			}
			var dup = false;
			for (var chk = 0; chk < idx; ++chk) {
				if (name == paramNames[chk]) {
					dup = true;
					break;
				}
			}
			if (dup) 
				continue;
			var value = '';
			me_subcase.store.each(function(param) {
				if (param.get('name') == name){
					value = param.get('value');
					return;
				}
			});
			var paramModel = Ext.ModelManager.create({name:name, value:value}, 'Param');
			data.push(paramModel);
		}
		me_subcase.store.loadData(data, false);
	}
});
